package com.xing.asm.simple2;

import com.xing.asm.simple.MyClassLoader;
import org.objectweb.asm.*;
import org.objectweb.asm.commons.AdviceAdapter;
import org.objectweb.asm.commons.Method;

import java.io.FileOutputStream;

public class FooMain {

    public static void main(String[] args) throws Exception{
        ClassReader classReader = new ClassReader(Foo.class.getName());
        ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS);
        classReader.accept(new MethodWriter(classWriter), ClassReader.EXPAND_FRAMES);

        byte[] code = classWriter.toByteArray();

        FileOutputStream fos = new FileOutputStream("E:\\ljx\\asm\\FooNew.class");
        fos.write(code);
        fos.close();

        MyClassLoader myClassLoader = new MyClassLoader();
        Class asmSimpleClass = myClassLoader.defineClass(null, code);
        java.lang.reflect.Method method = asmSimpleClass.getMethod("excute", new Class[]{String.class, int.class});
        Object asmSimpleInstance = asmSimpleClass.newInstance();
        method.invoke(asmSimpleInstance, new Object[]{"i ma God!", 1});
    }

}
